package com.qiantu.phone.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.MessageBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.action.StatusAction;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.app.TitleBarFragment;
import com.qiantu.phone.ui.adapter.MessageAdapter;
import com.qiantu.phone.widget.StatusLayout;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import okhttp3.Call;

/**
 * time   : 2021/12/22
 */
public final class MessageListFragment extends TitleBarFragment<AppActivity>
        implements OnRefreshLoadMoreListener, StatusAction {

    /**
     * 全部 : 0
     * 智能 : 1
     * 警报 : 2
     */
    private int type;
    private int pageIndex = 1;
    private int pageSize = 10;
    //1初始化  2刷新  3加载更多
    private int requestType = 1;

    public static MessageListFragment newInstance(int type) {
        MessageListFragment fragment = new MessageListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    private StatusLayout mStatusLayout;
    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView mRecyclerView;
    private MessageAdapter mMessageAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView() {
        type = getInt("type");
        mStatusLayout = findViewById(R.id.status_layout);
        smartRefreshLayout = findViewById(R.id.smart_refresh_layout);
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mMessageAdapter = new MessageAdapter(getAttachActivity());
        mMessageAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {

            }
        });
        mRecyclerView.setAdapter(mMessageAdapter);
        smartRefreshLayout.setOnRefreshLoadMoreListener(this);
        showLoading();
    }

    @Override
    protected void initData() {
        //获取通知消息
        LLHttpManager.getMessages(this, pageIndex, pageSize, type, AppApplication.getInstance().getCurrentHouseSerialNo(), new HttpCallback<HttpData<MessageBean>>(null) {

            @Override
            public void onSucceed(HttpData<MessageBean> result) {
                super.onSucceed(result);
                if (result != null && result.getData() != null) {
                    switch (requestType){
                        case 1:
                            mMessageAdapter.setData(result.getData().getList());
                            break;
                        case 2:
                            mMessageAdapter.clearData();
                            mMessageAdapter.setData(result.getData().getList());
                            break;
                        case 3:
                            mMessageAdapter.addData(result.getData().getList());
                            mMessageAdapter.setLastPage(result.getData().getList() == null || result.getData().getList().size() < 10);
                            break;
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
                switch (requestType){
                    case 1:
                        if (mMessageAdapter.getData() == null || mMessageAdapter.getData().size() == 0){
                            showEmpty(R.attr.img_emptystate_nolog, R.string.no_message, R.string.add, null);
                        }else {
                            showComplete();
                        }
                        break;
                    case 2:
                        smartRefreshLayout.finishRefresh();
                        break;
                    case 3:
                        smartRefreshLayout.finishLoadMore();
                        smartRefreshLayout.setNoMoreData(mMessageAdapter.isLastPage());
                        break;
                }
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        requestType = 2;
        pageIndex = 1;
        initData();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        requestType = 3;
        pageIndex = (int) Math.ceil(((float) mMessageAdapter.getData().size() / pageSize)) + 1;
        initData();
    }

    @Override
    public StatusLayout getStatusLayout() {
        return mStatusLayout;
    }
}